<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { ChallengeApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '130px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    SessionList:[] as Array<any>,
    skillList:[] as Array<any>,
    poolList:[] as Array<any>,
  })
  interface formQuert {
    id:number
    item_id:string
    draw_num:string
    rate:string
    skill_reward:Array<any>
  }
  let form = reactive({} as formQuert)
  let listQuery = reactive({
    game_id:''
  })
  const formRules = {
    draw_num: [
      { required: true, message: '请输入参与次数', trigger: 'blur' }
    ],
    item_id: [
      { required: true, message: '请选择技能类型', trigger: 'blur' }
    ],
    rate: [
      { required: true, message: '请输入概率', trigger: 'blur' }
    ],
    skill_reward: [
      { required: true, message: '请选择人物', trigger: 'blur' }
    ],
  }
  const getpoolList = async()=>{
    action.poolList = []
    action.skillList = []
    const res = await ChallengeApi.poolList(listQuery)
    const { data=[] } = res
    action.poolList = data.filter((item:any)=> item.item_type == 1)
    action.skillList = data.filter((item:any)=> item.item_type == 2)
  }
  const getSession = async()=>{
    if(action.SessionList.length > 0) return
      const res = await ChallengeApi.gameSwitch()
      const { data } = res
      if(!listQuery.game_id) {
        listQuery.game_id = data[0].game_id || ''
      }
      action.SessionList = data
  }
  const changeGameID = ()=>{
    form.item_id = ''
    getpoolList()
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      const {id=0,skill_reward,rate,draw_num,item_id} = form
      let submitSet = false
      const formQuery = {
        draw_num,
        skill_reward:JSON.stringify(skill_reward),
        rate,
        item_id
      }
      if(id) {
        await ChallengeApi.skillEdit({
          id,
          ...formQuery
        }).then(res=>{
          submitSet = true
          ToastSuccess('修改成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await ChallengeApi.skillAdd(formQuery).then(()=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = (query:any = {})=> {
    const {
      id=0,
      draw_num='',
      rate='',
      item_id='',
      skill_reward=[],
      game_id=''
    } = query
    let formQuery = {
      draw_num,
      rate,
      item_id,
      skill_reward,
    } as formQuert
    if(id) formQuery['id'] = id
    Object.assign(form,formQuery)
    listQuery.game_id = game_id
    const loading = Toastloading()
    getSession()
    getpoolList()
    loading.close()
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="5vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="55%"
      :title="`${form.id?'修改':'新增'}技能配置`">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <template v-if="!form.id">
          <el-form-item :label-width="formLabelWidth" label="游戏类型：">
            <el-select @change="changeGameID" v-model="listQuery.game_id" placeholder="请选择">
              <el-option
                v-for="item in action.SessionList"
                :key="item.game_id"
                :label="item.game_name"
                :value="item.game_id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="技能类型：" prop="item_id">
            <el-select v-model="form.item_id" placeholder="请选择">
              <el-option
                v-for="item in action.skillList"
                :key="item.id"
                :label="item.item_name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </template>
        <el-form-item :label-width="formLabelWidth" label="参与次数" prop="draw_num">
          <el-input v-model="form.draw_num" @input="allNumber(form,'draw_num')" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="概率(百分位)" prop="rate">
          <el-input v-model="form.rate" @input="allNumber(form,'rate')" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="人物选择" prop="skill_reward">
          <el-select
            multiple
            filterable
            v-model="form.skill_reward" placeholder="请选择">
            <el-option
              v-for="item in action.poolList"
              :key="item.id"
              :label="item.item_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
